package com.mstore.action;

import java.util.List;
import java.util.Map;

import com.mstore.pojo.Account;
import com.mstore.pojo.Question;
import com.mstore.pojo.Reply;
import com.mstore.service.QuestionService;
import com.mstore.service.ReplyService;
import com.opensymphony.xwork2.ActionContext;
/**
 * 
 * 
 *自动回复
 */
public class ReplyAction extends BaseAction {

	private static final long serialVersionUID = -4322801335063351590L;

	private String result;

	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	private ReplyService replyService;

	private QuestionService questionService;

	private List<Reply> replies;

	private Reply reply;

	/**
	 * 查询全部回复
	 * @return
	 */
	public String watchReply() {
		Object object = session.get("account");
		if (object == null) {
			result = ERROR_TIP;
			return "error";
		}
		Account account = (Account) object;
		reply = new Reply();
		reply.setEaId(account.getId());
		replies = replyService.watchReply(reply);
		return "toWatchReply";
	}

	/**
	 * 查询全部回复
	 * @return
	 */
	public String addReply() {
		Object object = session.get("account");
		if (object == null) {
			result = ERROR_TIP;
			return "error";
		}
		Account account = (Account) object;
		reply.setEaId(account.getId());
		reply.setCreateTime(getCreateTime());

		String content = reply.getContent();
		reply.setType(1); // 文本类型
		if (content.toLowerCase().contains("http://")) {
			reply.setType(2); // 链接类型
		}

		replyService.saveReply(reply);

		return "addSuccess";
	}

	/**
	 * 跳转到修改回复页面
	 * @return
	 */
	public String initModifyReply() {
		reply = replyService.getOneReply(reply);
		return "toModifyReply";
	}

	/**
	 * 修改回复
	 * @return
	 */
	public String modifyReply() {
		replyService.modifyReply(reply);

		return "modifySuccess";
	}

	/**
	 * 删除回复
	 * @return
	 */
	public String delReply() {

		Object object = session.get("account");
		if (object == null) {
			result = ERROR_TIP;
			return "error";
		}
		Account account = (Account) object;
		Question question = new Question();
		question.setEaId(account.getId());
		question.setRelyId(reply.getId());

		List<Question> questions = questionService
				.findQuestionsByReplyId(question);
		for (Question ques : questions) {
			questionService.delQuestion(ques);
		}

		replyService.delReply(reply);
		return "delSuccess";
	}

	/**
	 * 添加关注时回复
	 * @return
	 */
	public String addSubscribeReply() {
		Object object = session.get("account");
		if (object == null) {
			result = ERROR_TIP;
			return "error";
		}
		Account account = (Account) object;
		reply.setEaId(account.getId());
		reply.setCreateTime(getCreateTime());
		reply.setType(4); // type=4 关注时回复

		replyService.saveReply(reply);

		return "addSubscribeSuccess";
	}

	/**
	 * 查看关注时回复
	 * @return
	 */
	public String watchSubscribeReply() {
		Object object = session.get("account");
		if (object == null) {
			result = ERROR_TIP;
			return "error";
		}
		Account account = (Account) object;
		reply = new Reply();
		reply.setEaId(account.getId());

		reply = replyService.watchSubscribeReply(reply);

		if (reply == null) {
			return "addSubscribe";
		}

		return "toWatchSubscribeReply";
	}

	/**
	 * 修改关注时回复
	 * @return
	 */
	public String modifySubscribeReply() {
		replyService.modifySubscribeReply(reply);
		return "modifySubscribeSuccess";
	}
	/**
	 * 跳转到修改关注时回复页面
	 * @return
	 */
	public String initModifySubscribeReply() {
		reply = replyService.getOneReply(reply);
		return "toModifySubscribeReply";
	}

	/**
	 * 删除关注时回复
	 * @return
	 */
	public String delSubscribeReply() {
		replyService.delReply(reply);
		return "delSubscribeReplySuccess";
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public ReplyService getReplyService() {
		return replyService;
	}

	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}

	public List<Reply> getReplies() {
		return replies;
	}

	public void setReplies(List<Reply> replies) {
		this.replies = replies;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	public QuestionService getQuestionService() {
		return questionService;
	}

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

}
